<?php

namespace app\model;

use app\api\service\WechatSubscribeService;
use think\facade\Queue;
use think\Model;
use think\model\relation\BelongsTo;

class VideoTemplate extends Model
{
    // 设置当前模型对应的数据表
    protected $name = 'video_template';
    // 设置当前模型主键
    protected $pk = 'id';

    // 设置JSON数据返回数组
    protected $jsonAssoc = true;
    //获取器
    protected $append = [
        'video_arr'
    ];
    //对video_ids转为数组
    public function getVideoArrAttr($value, $data){
        $value=isset($data['video_ids'])?$data['video_ids']:'';
        if(empty($value)){
            return [];
        }
        $arr=explode(',',$value);
        return $arr;
    }
    /**
     * 根据条件获取数据
     * 
     * @dateTime 2022/09/02
     * @author zhixiang0215@126.com
     * @param string fields
     * @param array where
     * @param string order
     * @return array
     */
    public function getInfoByWhere($where = [], string $fields = 'id', string $order = 'sort asc')
    {
        $data = $this::where($where)->field($fields)->order($order)->find();
        return $data ? $data->toArray() : [];
    }


    /**
     * 根据Id获取数据
     * 
     * @dateTime 2022/09/02
     * @author zhixiang0215@126.com
     * @param int id
     * @param string fields
     * @return array
     */
    public function getInfoById($id, $fields = 'id')
    {
        $data = $this::where(['id' => $id])->field($fields)->find();
        return $data ? $data->toArray() : [];
    }

    /**
     * 根据Ids获取数据
     * 
     * @dateTime 2022/09/02
     * @author zhixiang0215@126.com
     * @param array ids
     * @param string fields
     * @return array
     */
    public function getInfoByIds(array $ids, string $fields = 'id', string $order = 'id desc')
    {
        $data = $this::where(['id' => $ids])->field($fields)->order($order)->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 根据videoIds获取数据
     * 
     * @dateTime 2022/09/02
     * @author zhixiang0215@126.com
     * @param array videoIds
     * @param string fields
     * @return array
     */
    public function getInfoByVideoIds(array $videoIds, string $fields = 'id', string $order = 'id desc', string $group = '')
    {
        $model = $this::where(['video_id' => $videoIds])->field($fields);
        if ($group) {
            $model->group($group);
        }
        $data = $model->order($order)->select();
        return $data ? $data->toArray() : [];
    }

    // 根据条件获取分页数据
    public function getPagingDataByWhere($where = [], $page = 1, $limit = 10, $fields = 'id, name', $order = 'id asc')
    {
        $total = $this::where($where)->count();

        if ($total == 0) {
            return ['total' => 0, 'total_page' => 0, 'data' => []];
        }

        $offset = ($page - 1) * $limit;
        $totalPage = ceil($total / $limit);

        $data = $this::where($where)->field($fields)->order($order)->limit($offset, $limit)->select()->toArray();

        return ['total' => $total, 'total_page' => $totalPage, 'data' => $data];
    }

    /**
     * 添加/编辑
     * 
     * @dateTime 2022/09/19
     * @author zhixiang0215@126.com
     * @param array $params
     * @return int
     */
    public function edit(array $params)
    {
        if (empty($params['id'])) {
            $data = $this::create($params);
            return $data->id;
        }
        $this::update($params);
        return $params['id'];
    }

    /**
     * 根据条件获取数据
     * 
     * @dateTime 2022/09/02
     * @author zhixiang0215@126.com
     * @param string fields
     * @param array where
     * @param string order
     * @return array
     */
    public function getDataByWhere($where = [], string $fields = 'id', $order = 'id asc')
    {
        $data = $this::where($where)->field($fields)->order($order)->select();
        return $data ? $data->toArray() : [];
    }
}